package com.lxh.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.lxh.student.bo.SelectCourseBo;
import com.lxh.student.common.ResponseResult;
import com.lxh.student.dao.RcCourseMapper;
import com.lxh.student.dao.RcStudentMapper;
import com.lxh.student.entity.RcCourse;
import com.lxh.student.entity.RcStudent;
import com.lxh.student.entity.RcStudentCourse;
import com.lxh.student.dao.RcStudentCourseMapper;
import com.lxh.student.service.RcStudentCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxh.student.vo.output.StudentCourseVo;
import javassist.runtime.Cflow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LXh
 * @since 2022-10-24
 */
@Service
public class RcStudentCourseServiceImpl extends ServiceImpl<RcStudentCourseMapper, RcStudentCourse> implements RcStudentCourseService {

    @Autowired
    private RcCourseMapper courseMapper;
    @Autowired
    private RcStudentMapper studentMapper;
    @Autowired
    private RcStudentCourseMapper scMapper;

    @Override
    public ResponseResult addStudentCourse(Integer courseId, Integer studentId) {
        //课程是否存在 ，查询这个给课程年级的值 ，存在就通过
        RcCourse course = courseMapper.selectOne(new QueryWrapper<RcCourse>().eq("course_id", courseId));
        if(course==null){
            //没有这个课程
            return ResponseResult.createFail("该课程不存在");
        }

        //学员id正确
        // 查询 学员和 班级 链表查询 年级的值  有值就代表存在这个学员
        Integer studentGrade = studentMapper.getStudentClassGrade(studentId);
        if(studentGrade==null){
            //学员查询不到班级相关数据，该学院不存在
            return ResponseResult.createFail("该学员不存在");
        }

        //利用上面两个数据判断  学员的年级和课程的年级是否一致
        if(!studentGrade.equals(course.getCourseGrade())){
            //课程年级和学员的年级不一致
            return ResponseResult.createFail("该课程年级不同，无法选修");
        }


        //判断学员是否已经有选过这个课程
        RcStudentCourse rcStudentCourse = scMapper.selectOne(
                new QueryWrapper<RcStudentCourse>()
                        .eq("sc_student_id", studentId)
                        .eq("sc_course_id", courseId));
        if(rcStudentCourse != null){
            //学员已有这个课程
            return ResponseResult.createFail("该课程已有选修，无法重复选择");
     }

        //判断课程人数是否以达到最大
        RcCourse courseCount = courseMapper.selectOne(
                new QueryWrapper<RcCourse>()
                        .eq("course_id", courseId)
                        .apply("course_selected_count<course_max_size")
        );
        if (courseCount ==null){
            //该课程选秀人数已满
            return ResponseResult.createFail("该课程选修人数已满");
        }


        //判断选修的课程上课时间是否有冲突
        //查询到学员 现在已有的上课时间
        List<String> courseTimeList = scMapper.getStudentCourseTime(studentId);
        //选修课时间
        String courseTime = course.getCourseTime();
        for (String s: courseTimeList){
            //课程是否有时间重复
            if(SelectCourseBo.check(courseTime,s)){
                return ResponseResult.createFail("所选课程存在时间冲突");
            }
        }

        //新增
        RcStudentCourse studentCourse = new RcStudentCourse(studentId,courseId);
        if(scMapper.insert(studentCourse)>0){
            //修改被选修课程 人数加1
            //使用已查询好的课程数据对象进行修改
            course.setCourseSelectedCount(course.getCourseSelectedCount() + 1);
            courseMapper.updateById(course);
            return  ResponseResult.createSuccess("课程选修成功");
        }
        return  ResponseResult.createFail("课程选修失败");

    }

    //退选课程

    @Override
    public ResponseResult delCourse(Integer scId) {
        //使用 查询课程id
        RcStudentCourse studentCourse = scMapper.selectOne(new QueryWrapper<RcStudentCourse>().eq("sc_id", scId));
        //使用课程id 获取这个课程数据
        RcCourse rcCourse = courseMapper.selectById(studentCourse.getScCourseId());
        //修改选修人数
        rcCourse.setCourseSelectedCount(rcCourse.getCourseSelectedCount()-1);
        //更新选修人数
        courseMapper.updateById(rcCourse);
        //删除学员选修数据
        if(scMapper.delete(new QueryWrapper<RcStudentCourse>().eq("sc_id", scId))>0){
            return ResponseResult.createSuccess("退选成功");
        }
        return ResponseResult.createFail("退选失败");
    }


    //获取已选课程的数据
    @Override
    public ResponseResult getStudentCourse(Integer studentId) {
        List<StudentCourseVo> list = scMapper.getStudentCourse(studentId);
        return  ResponseResult.createSuccess(list);
    }


    //查询课表

    @Override
    public ResponseResult showCourseTimeTable(Integer studentId) {
        return ResponseResult.createSuccess(scMapper.getCourseTimeTable(studentId));
    }

    //分数查询

    @Override
    public ResponseResult getStudentScore(Integer studentId) {
        return  ResponseResult.createSuccess(scMapper.getStudentScore(studentId));
    }
}
